package com.godz.frontData.index;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.godz.mapper.indexMyStoryMapper;
import com.godz.pojo.indexMyStory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author GodzIINeo2023
 */
@RestController
public class storyData {
    final
    indexMyStoryMapper myStoryMapper;

    public storyData(indexMyStoryMapper myStoryMapper) {
        this.myStoryMapper = myStoryMapper;
    }

    //查询
    @GetMapping("/index/get/indexMyStory")
    public List<indexMyStory> getStory(@RequestParam(value = "pageNow", defaultValue = "1") int pageNow) {
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("story_date");
        Page<indexMyStory> page = new Page<>(pageNow, 10);
        Page<indexMyStory> myStoryPage = myStoryMapper.selectPage(page, wrapper);
        return myStoryPage.getRecords();
    }
    //得到总共有多少页
    @GetMapping("/index/get/indexMyStory/pageAll")
    public String getStoryPage() {
        Integer count = myStoryMapper.selectCount(null);
        return count.toString();
    }
    //查询重要事件
    @GetMapping("/index/get/indexMyStoryI")
    public List<indexMyStory> getStoryI(@RequestParam(value = "pageNow", defaultValue = "1") int pageNow) {
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<>();
        wrapper.eq("focus", 1)
               .orderByDesc("story_date");
        Page<indexMyStory> page = new Page<>(pageNow, 10);
        Page<indexMyStory> myStoryPage = myStoryMapper.selectPage(page, wrapper);
        return myStoryPage.getRecords();
    }
    //重要事件总共有多少页
    @GetMapping("/index/get/indexMyStoryI/pageAll")
    public Integer getStoryPageI() {
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<>();
        wrapper.eq("focus", 1);
        return myStoryMapper.selectCount(wrapper);
    }
    //查询单条信息
    @GetMapping("/index/get/indexMyStory/{intId}")
    public indexMyStory getOneStory(@PathVariable("intId") Integer integer) {
        return myStoryMapper.selectOne(
                new QueryWrapper<indexMyStory>().eq("id", integer)
        );
    }
    //增加一条
    @PostMapping("/index/post/addStory")
    public String addStory(@RequestBody indexMyStory story) {
        myStoryMapper.insert(story);
        return "insert OK";
    }
    //修改一条
    @PostMapping("/index/post/editStory")
    public String editStory(@RequestBody indexMyStory story) {
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<>();
        wrapper.eq("id", story.getId());

        indexMyStory story1 = new indexMyStory();

        story1.setStoryTitle(story.getStoryTitle());
        story1.setHumorTitle(story.getHumorTitle());
        story1.setStorySite(story.getStorySite());
        story1.setStoryDate(story.getStoryDate());
        story1.setStoryFirst(story.getStoryFirst());
        story1.setStorySecond(story.getStorySecond());
        story1.setStoryThird(story.getStoryThird());
        story1.setStorySummary(story.getStorySummary());
        story1.setStoryImg1(story.getStoryImg1());
        story1.setStoryImg2(story.getStoryImg2());
        story1.setStoryImg3(story.getStoryImg3());
        story1.setFocus(story.getFocus());

        myStoryMapper.update(story1, wrapper);
        return "Update OK";
    }
    //删除一条
    @GetMapping("/index/get/deleteStory/{id}")
    public String deleteStory(@PathVariable("id") Integer integer) {
        myStoryMapper.deleteById(integer);
        return "Delete Ok";
    }
    //根据id和标题信息查询内容
    @GetMapping("/queryStory/{title}")
    public List<indexMyStory> queryStoryByTitle(@PathVariable("title") String title) {
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<indexMyStory>()
                .like("story_title", title)
                .or()
                .eq("id", title);
        return myStoryMapper.selectList(wrapper);
    }
    //根据id和标题查询重要信息
    @GetMapping("/queryStoryI/{title}")
    public List<indexMyStory> queryStoryByTitleI(@PathVariable("title") String title) {
        QueryWrapper<indexMyStory> wrapper = new QueryWrapper<indexMyStory>()
                .like("story_title", title)
                .or()
                .eq("id", title)
                .and(wrapper1 -> wrapper1.eq("focus", 1));
        return myStoryMapper.selectList(wrapper);
    }
}
